<?php
class Layout extends ORM
{
	public $table = 'layouts';
	
	public $has_one = array(
		'theme',
		'parent' => array(
	        'class' => 'layout',
	        'other_field' => 'child'
	    )	
	);
	
	public $has_many = array(
		'category',
		'child' => array(
			'class' => 'layout',
	        'other_field' => 'parent'
	    )
	);
	
	public function __construct($id = NULL)
	{
		parent::__construct($id);
	}
	
	public function option($theme_id, $layout_id = NULL)
	{
		$result = $this->where('theme_id', $theme_id)->where('parent_id', 0)->order_by('id asc')->get();
		$html = '<select name="layout_id">';
		foreach($result as $layout)
		{
			$html .= '<optgroup label="'.$layout->description.'">';
			foreach($layout->child as $item)
			{
				$selected = ($layout_id == $item->id) ? 'selected' : '';
				$html .= '<option value="'.$item->id.'" '.$selected.' >'.$item->description.'</option>';
			}
			$html .= '</optgropu>'; 
		}
		$html .= '</select>';
		return $html;
	}
}